<template>
	<view class="container">
		<uni-forms>
			<uni-forms-item class="form-item" label="监测站点代码*" label-width="230">
				<uni-easyinput type="text" :disabled="true" v-model="ydNumber" />
			</uni-forms-item>
			<view class="container-pd" v-for="(item,index) in frequencys" :key="index" :index="String(index)">
				<uni-forms-item class="form-item" :label="item.zwmc" label-width="230">
					<view class="tag-view" v-for="(tag,inkey) in item.tags" :key="inkey">
						<uni-tag v-if="tag.sfcx=='是'" :text="'样地'+tag.yfNumber" type="primary"
							@click="chane(false,index,inkey)" />
						<uni-tag v-else :text="'样地'+tag.yfNumber" type="info" @click="chane(true,index,inkey)" />

						<image class="item-image" src="../../../static/image/minus.png" width="30" height="30"
							@click="delTag(index,inkey)"></image>

					</view>
				</uni-forms-item>
				<uni-forms-item class="form-item" label="次数" label-width="230">
					<uni-easyinput type="text" :disabled="true" v-model="item.count" />
				</uni-forms-item>
				<uni-forms-item class="form-item" label="频度" label-width="230">
					<uni-easyinput type="text" :disabled="true" v-model="item.pc" />
				</uni-forms-item>
			</view>
			<!-- 	<uni-forms-item  class="form-item" label="异针茅" label-width="230" >
				<view class="tag-view" v-for="(item,index) in tags " :key="index">
					<uni-tag :text="'样地'+item"  type="primary"  /><image class="item-image" src="../../../static/image/minus.png" width="30" height="30"></image>
				</view>
			</uni-forms-item>
			<uni-forms-item class="form-item" label="次数" label-width="230" >
				<uni-easyinput type="text" v-model="frequency.zdbm" />
			</uni-forms-item>
			<uni-forms-item class="form-item" label="频度" label-width="230" >
				<uni-easyinput type="text" v-model="frequency.zdbm" />
			</uni-forms-item> -->
			<button type="primary" @click="submit()">{{title}}</button>
		</uni-forms>

	</view>
</template>

<script>
	import {
		getInfo,
		setInfo
	} from '@/core/unicommon/storyageInfo.js'
	import {
		addFrequencys,
		userinfo
	} from '@/api/system.js'

	export default {
		data() {
			return {
				title: "提交",
				tags: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
				frequency: {
					zdbm: "",
				},
				frequencys: [],
				ydNumber: "",
				network: false,
				userdata: null

			}
		},
		onLoad(e) {

			if (e && e.data) {
				let data = JSON.parse(decodeURIComponent(e.data))
				console.log("b", data)
				this.ydNumber = data.ydNumber
				getInfo("userdata").then(res => {
					this.userdata = res.data
					this.getFrequency(res.data.frequency, res.data.grassType)
				})
			}
			this.getNetWork()
		},
		methods: {
			getNetWork() {
				let that = this
				uni.getNetworkType({
					success: res => {
						if (res.networkType === 'none') {
							that.network = false
						} else {
							that.network = true
						}
					}
				})
			},
			getCount(datas) {
				let data = datas.filter(d => d.sfcx == '是')
				if (data) {
					return data.length
				} else {
					return 0
				}

			},
			getfre(datas) {
				return this.getCount(datas) / datas.length
			},
			getFrequency(frequency, grassType) {
				grassType.forEach(d => {
					if (d.ydNumber == this.ydNumber) {
						let fre = frequency.filter(t => t.zwmc == d.zwmc && t.ydNumber == this.ydNumber)
						let isExit = this.frequencys.find(t => t.zwmc == d.zwmc)

						if (!isExit) {
							if (!fre || fre.length == 0) {

								let newdata = {
									"zwmc": d.zwmc,
									"tags": [],
									count: 0,
									pc: 0

								}
								for (let i = 1; i < 11; i++) {
									let data = {
										zwmc: d.zwmc,
										sfcx: "否",
										ydNumber: this.ydNumber,
										yfNumber: i,
										"action": "creat"
									}
									newdata.tags.push(data)
								}
								this.frequencys.push(newdata)
							} else {
								let olddata = {
									zwmc: d.zwmc,
									tags: fre,
									count: fre.filter(t => t.sfcx == '是').length,
									pc: fre.filter(t => t.sfcx == '是').length / fre.length
								}
								this.frequencys.push(olddata)
							}

						}
					}

				})
			},
			getTags(sfcx) {
				console.log(sfcx, sfcx.split(','))
				return sfcx.split(',')
			},
			delTag(index, inkey) {
				console.log(index, inkey)
				// let data = this.frequencys[index]
				// data.tags = data.tags.splice(inkey, 1)
				// data.sfcx = data.tags.join(',')
				// data.action = "edit"
				// this.frequencys[index] = data
				// console.log(this.frequencys, data, index, inkey)
				// 创建 tags 数组的副本
				let data = this.frequencys[index];
				data.tags = [...data.tags];

				// 删除指定索引的标签
				data.tags.splice(inkey, 1);


				// 标记项目为已编辑
				data.action = "edit";
				data.count = data.tags.filter(t => t.sfcx == '是').length
				data.pc = data.tags.filter(t => t.sfcx == '是').length / data.tags.length
				// 确保 Vue 的响应式系统跟踪变化
				this.$set(this.frequencys, index, data);

			},
			chane(checked, index, inkey) {
				console.log(checked, index, inkey)

				let data = this.frequencys[index];


				let tags = data.tags[inkey]
				// 删除指定索引的标签
				if (checked) {
					tags.sfcx = '是'
				} else {
					tags.sfcx = '否'
				}
				data.tags[inkey] = tags
				data.tags = [...data.tags];

				// 标记项目为已编辑
				data.action = "edit";
				data.count = data.tags.filter(t => t.sfcx == '是').length
				data.pc = data.tags.filter(t => t.sfcx == '是').length / data.tags.length

				// 确保 Vue 的响应式系统跟踪变化
				this.$set(this.frequencys, index, data);

			},
			submit() {
				let frequency = []
				this.frequencys.forEach(d => {
					frequency = frequency.concat(d.tags)
				})
				if (this.network) {

					addFrequencys(frequency).then(res => {

					})
				}
				let fres = this.userdata.frequency.filter(t => t.ydNumber != this.ydNumber)
				this.userdata.frequency = fres.concat(frequency)
				uni.showToast({
					title: '提交成功',
					icon: 'none'
				})
				uni.navigateBack({
				    delta: 1
				});



			}
		}
	}
</script>

<style scoped>
	.form-item {
		padding: 10rpx;

	}

	.tag-view {
		float: left;
		display: flex;
		margin: 10rpx;
	}

	.item-image {
		width: 60rpx;
		height: 60rpx;
	}
</style>